package com.demo.jvm.structure.constant.parser;

import com.demo.jvm.structure.constant.CONSTANT_Utf8_info;
import com.demo.jvm.structure.constant.CONSTANT_info;

import java.io.DataInputStream;
import java.nio.charset.StandardCharsets;

/**
 * CONSTANT_Utf8_info {
 * u1 tag;
 * u2 length;
 * u1 bytes[length];
 * }
 */
public class CONSTANT_Utf8_info_Parser {

    private DataInputStream dis;

    public CONSTANT_Utf8_info_Parser(DataInputStream dis) {
        this.dis = dis;
    }

    public CONSTANT_info readValue(int tableIndex) {
        try {
            int length = dis.readUnsignedShort();
            byte[] bytes = new byte[length];
            dis.read(bytes);
            return new CONSTANT_Utf8_info((byte) 1, tableIndex, new String(bytes, StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
